<template>
  <div class="internal">
    <navbar :text="$route.params.text"></navbar>
    <div class="fillMain">
      <div class="fillInfo">
        <img
          src="https://cdn7.axureshop.com/demo/1763441/images/%E6%97%A0%E5%B0%B1%E8%AF%8A%E5%8D%A1/u1274.png"
          alt=""
        />
        <div class="fillImg">
          <img
            src="https://cdn7.axureshop.com/demo/1763441/images/%E6%97%A0%E5%B0%B1%E8%AF%8A%E5%8D%A1/u1271.png"
            alt=""
          />
          <span>填写信息</span>
        </div>
        <img
          src="https://cdn7.axureshop.com/demo/1763441/images/%E6%97%A0%E5%B0%B1%E8%AF%8A%E5%8D%A1/u1274.png"
          alt=""
        />
      </div>
      <form class="fillText">
        <div class="fillTop">
          <div class="fillName">
            <span>真实姓名<b>*</b></span>
            <input
              type="text"
              required
              placeholder="请填写您的真实姓名"
              v-model="name"
            />
          </div>
          <div class="fillIDcard">
            <span>身份证号<b>*</b></span>
            <input
              type="text"
              required
              placeholder="请填写您的身份证号"
              v-model="card"
              maxlength="18"
            />
          </div>
          <div class="fillSex">
            <span>性别<b>*</b></span>
            <label for=""
              ><input
                type="radio"
                name="sex"
                value="男"
                v-model="sex"
              />男</label
            >
            <label for=""
              ><input
                type="radio"
                name="sex"
                value="女"
                v-model="sex"
              />女</label
            >
          </div>
          <div class="fillDate">
            <span>出生日期<b>*</b></span>
            <p @click="show = true">
              <input
                type="text"
                v-model="date"
                placeholder="请选择您的出生日期"
                required
              />
              <img
                src="https://cdn7.axureshop.com/demo/1763441/images/%E9%80%89%E6%8B%A9%E6%97%B6%E6%AE%B5/u514.png"
                alt=""
              />
            </p>
          </div>
          <div class="fillPhone">
            <span>联系电话<b>*</b></span>
            <input
              type="tel"
              required
              placeholder="请填写您的联系电话"
              v-model="tel"
              maxlength="11"
            />
          </div>
          <div class="fillSite">
            <span>现居地</span>
            <input type="text" placeholder="请填写现居地" v-model="site" />
          </div>
          <div class="fillSocial">
            <span>社保卡</span>
            <input
              type="number"
              placeholder="请填写社保卡号"
              v-model="social"
            />
          </div>
        </div>
        <div class="fillBottom">
          <div class="fillRelate">
            <span>与本人关系<b></b></span>
            <p @click="show3 = true">
              <input
                type="text"
                v-model="relate"
                placeholder="请选择和您的关系"
              />
              <img
                src="https://cdn7.axureshop.com/demo/1763441/images/%E9%80%89%E6%8B%A9%E6%97%B6%E6%AE%B5/u514.png"
                alt=""
              />
            </p>
          </div>
          <div class="fillDefault">
            <span>设为默认</span>
            <div class="defaultbutton">
              <van-switch
                v-model="checked"
                active-color="#2ec7c9"
                inactive-color="#ccc"
                size="20px"
                @change="defaultAciton"
              />
            </div>
          </div>
        </div>
        <div class="submit">
          <van-button
            type="primary"
            size="large"
            color="#2ec7c9"
            @click.prevent="submitAciton"
            >提交</van-button
          >
        </div>
      </form>
    </div>
    <van-overlay :show="show" @click="show = false">
      <div class="wrapper" @click.stop></div
    ></van-overlay>
    <van-datetime-picker
      v-if="show"
      v-model="currentDate"
      type="year-month"
      title="选择出生年月"
      :min-date="minDate"
      :max-date="maxDate"
      :formatter="formatter"
      @confirm="confirmAciton"
      @cancel="cancelAciton"
    />
    <van-action-sheet
      v-model="show3"
      :actions="actions"
      cancel-text="取消"
      close-on-click-action
      @cancel="onCancel"
      @select="sectionAction"
      title="请选择与本人关系"
    />
  </div>
</template>

<script>
import navbar from "../../../components/navbar.vue";
// import axios from "axios";
import { addCardApi } from "../../../api/patient";
import {
  DatetimePicker,
  Overlay,
  ActionSheet,
  Toast,
  Switch,
  Button,
} from "vant";
export default {
  components: {
    navbar,
    [DatetimePicker.name]: DatetimePicker,
    [Overlay.name]: Overlay,
    [ActionSheet.name]: ActionSheet,
    [Switch.name]: Switch,
    [Button.name]: Button,
  },
  data() {
    return {
      show: false,
      show2: false,
      show3: false,
      checked: false,
      name: "",
      card: "",
      sex: "",
      tel: "",
      date: "",
      site: "",
      social: "",
      relate: "",
      minDate: new Date(1920, 0, 1),
      maxDate: new Date(2023, 5, 22),
      currentDate: new Date(),
      default: false,
      actions: [
        { name: "本人" },
        { name: "父母" },
        { name: "夫妻" },
        { name: "姐妹" },
        { name: "兄弟" },
      ],
    };
  },
  methods: {
    // 提交表单获取响应式数据
    async submitAciton() {
      if (
        this.name == "" ||
        this.card == "" ||
        this.sex == "" ||
        this.tel == "" ||
        this.date == ""
      ) {
        Toast("存在必填信息未填写");
        return;
      } else if (this.card.length < 18 || this.tel.length < 11) {
        Toast("身份证号或手机号填写有误（请检查是否填写完整）");
        return;
      }
      await addCardApi({
        name: this.name,
        idcard: this.card,
        sex: this.sex,
        tel: this.tel,
        date: this.date,
        site: this.site,
        social: this.social,
        relate: this.relate,
      });
      Toast("提交成功");
      this.$router.push({ name: "FilingInstructions" });
    },
    cancelAciton() {
      this.show = false;
    },
    onCancel() {
      Toast("取消");
    },
    sectionAction(action) {
      this.relate = action.name;
    },
    defaultAciton(value) {
      this.default = value;
    },
    confirmAciton(value) {
      let year = value.getFullYear();
      let month = value.getMonth() + 1;
      this.date = `${year}年${month}月`;
      this.show = false;
    },
    formatter(type, val) {
      if (type === "year") {
        return `${val}年`;
      } else if (type === "month") {
        return `${val}月`;
      }
      return val;
    },
  },
};
</script>

<style lang="scss" scoped>
@function vw($px) {
  @return ($px/375) * 100vw;
}

* {
  margin: 0;
  padding: 0;
}

.fillMain {
  width: 100%;
  height: 100%;
  position: absolute;
  top: vw(55);
  z-index: 2;

  .fillInfo {
    width: 100%;
    height: vw(60);
    display: flex;
    justify-content: center;
    align-items: center;
    background: #fff;

    .fillImg {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;

      img {
        width: vw(30);
        height: vw(30);
        margin-bottom: vw(5);
      }

      span {
        font-size: vw(12);
        color: #2ec7c9;
      }
    }
  }

  .fillText {
    margin-top: vw(12);

    .fillTop {
      padding-left: vw(20);
      display: flex;
      background: #fff;
      flex-direction: column;
      height: auto;
      .fillSex {
        label {
          margin-right: vw(20);
          display: flex;
          align-items: center;
        }
      }
    }
    .fillBottom {
      margin-top: vw(10);
      padding-left: vw(20);
      display: flex;
      background: #fff;
      flex-direction: column;
      height: auto;
      justify-content: center;
      .defaultbutton {
        flex: 1;
        margin-left: vw(175);
      }
    }
    .submit {
      width: calc(100% - vw(70));
      height: vw(46);
      position: fixed;
      bottom: 0;
      padding: vw(5) vw(35);
      background: #fff;
      .van-button--large {
        height: vw(44);
      }
    }
    div {
      height: vw(38);
      width: calc(100% - vw(20));
      display: flex;
      align-items: center;
      font-size: vw(14);
      border-bottom: vw(1) solid #cccccc69;

      span {
        width: vw(100);
        b {
          color: red;
        }
      }

      input {
        border: none;
        flex: 1;
      }
      input::placeholder {
        font-size: vw(13);
        color: #ccc;
      }
      p {
        flex: 1;
        height: 100%;
        display: flex;
        justify-content: space-between;
        align-items: center;
        img {
          width: vw(10);
          height: vw(15);
          margin-right: vw(20);
        }
      }
    }
  }
}
.van-switch {
  width: vw(40) !important;
  height: vw(20) !important;
}
.van-overlay {
  position: fixed;
  bottom: vw(308);
  z-index: 5;
}
.van-picker {
  position: fixed;
  width: 100%;
  bottom: 0;
  z-index: 9;
}
.van-popup {
  width: 100%;
  position: fixed;
  bottom: 0;
  height: vw(400);
  .van-icon {
    display: none;
  }
  .van-action-sheet__header {
    color: #646464;
    border-bottom: vw(1) solid #cccccc82;
    font-size: vw(14);
  }
  .van-action-sheet__content {
    display: flex;
    flex-direction: column;

    .van-action-sheet__item {
      height: vw(70);
      color: #2ec7c9;
      border-bottom: vw(1) solid #cccccc82;
    }
  }
  .van-action-sheet__cancel {
    height: vw(50);
    color: #a3a3a3;
  }
}
</style>
